package org.tinygroup;

import org.apache.commons.dbcp.BasicDataSource;
import org.tinygroup.jdbctemplatedslsession.SimpleDslSession;
import org.tinygroup.jdbctemplatedslsession.daosupport.OrderBy;
import org.tinygroup.pojo.BuzIotCardDayReport;
import org.tinygroup.tinysqldsl.DslSession;
import org.tinygroup.tinysqldsl.Pager;
import org.tinygroup.tinysqldsl.Select;
import org.tinygroup.tinysqldsl.base.Column;
import org.tinygroup.tinysqldsl.base.Condition;
import org.tinygroup.tinysqldsl.base.Table;
import org.tinygroup.tinysqldsl.formitem.SubSelect;
import org.tinygroup.tinysqldsl.select.Join;


import static org.tinygroup.tables.BuzIotCardDayReportTable.BUZ_IOT_CARD_DAY_REPORT_TABLE;
import static org.tinygroup.tables.BuzIotCardTable.BUZ_IOT_CARD_TABLE;



import static org.tinygroup.tinysqldsl.formitem.SubSelect.subSelect;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        listCurrentStatusOfIotCardPage(0,10,1);
    }

    /**
     * 查询公司当前的卡的信息。<br>
     *
     * @param firmId
     * @return
     */
    public static Pager<BuzIotCardDayReport> listCurrentStatusOfIotCardPage(int start,
                                                                     int limit, Integer firmId, final OrderBy... orderArgs) {
        Table TA = new Table("A");
        Table TB = new Table("B");

        SubSelect subSelect1 = subSelect(Select
                .select(BUZ_IOT_CARD_DAY_REPORT_TABLE.MSISDN,
                        BUZ_IOT_CARD_DAY_REPORT_TABLE.REPORT_DATE.distinct().as(
                                "max_report_date",true))
                .from(BUZ_IOT_CARD_DAY_REPORT_TABLE)
                .groupBy(BUZ_IOT_CARD_DAY_REPORT_TABLE.MSISDN));
        Select<?> select = Select
                .select(TB.ALL)
                .from(subSelect1);
//        Condition condition = new Column(TB,
//                BUZ_IOT_CARD_DAY_REPORT_TABLE.DOSAGE_OF_GPRS.getColumnName())
//                .greaterThanEqual(25 * 1024);
//
//        select.where(condition);
        System.out.println("select.parsedSql():"+select.parsedSql());
        BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName("com.mysql.jdbc.Driver");
        ds.setUsername("root");
        ds.setPassword("123456");
        ds.setUrl("jdbc:mysql://127.0.0.1/bizframe2");
        DslSession dslSession = new SimpleDslSession(ds);
        return dslSession.fetchPage(select, start, limit, false,
                BuzIotCardDayReport.class);
    }
}
